<script lang="ts" setup>
import HyTable from '@/base-ui/table';
import { usePermissionHooks } from '@/hooks/use-permission';
import { useStore } from '@/store';
import { computed, ref, watch } from 'vue';

const props = defineProps({
  contentTableConfig: {
    type: Object,
    require: true
  },
  pageName: {
    type: String,
    required: true
  }
});

// 0.获取操作的权限
const isCreate = usePermissionHooks(props.pageName, 'create') || true;
const isUpdate = usePermissionHooks(props.pageName, 'update') || true;
const isDelete = usePermissionHooks(props.pageName, 'delete') || true;
const isQuery = usePermissionHooks(props.pageName, 'query') || true;

const store = useStore();
// 1.双向绑定pageInfo
const pageInfo = ref({ currentPage: 1, pageSize: 10 });
// 分页器变化
watch(pageInfo, () => getPageData());

// 2.发送请求获取数据
const getPageData = (queryInfo: any = {}) => {
  store.dispatch('systemModule/getPageListAction', {
    pageName: props.pageName,
    queryInfo: {
      offset: pageInfo.value.currentPage * pageInfo.value.pageSize,
      size: pageInfo.value.pageSize,
      ...queryInfo
    }
  });
};
getPageData();
// 3.从vuex获取数据
const dataList = computed(() => store.getters[`systemModule/pageListData`](props.pageName));
const dataCount = computed(() => store.getters[`systemModule/pageListCount`](props.pageName));

// 4.获取动态插槽
const otherPropsSlots = props.contentTableConfig?.propList.filter((item: any) => {
  if (!item.slotName) return false;
  const fixedSlotName = ['status', 'createAt', 'updateAt', 'handler'];
  if (fixedSlotName.includes(item.slotName)) return false;
  return true;
});
const handleDeleteClick = (item: any) => {
  console.log(item);
  store.dispatch('systemModule/deletePageDataAction', {
    pageName: props.pageName,
    id: item.id
  });
};
const emits = defineEmits(['newBtnClick', 'editBtnClick']);
const handleNewClick = () => {
  emits('newBtnClick');
};
const handleEditClick = (item: any) => {
  emits('editBtnClick', item);
};
defineExpose({
  getPageData
});
</script>
<template>
  <div class="page-content">
    <hy-table
      :listData="dataList"
      :dataCount="dataCount"
      v-bind="contentTableConfig"
      v-model:pageInfo="pageInfo"
    >
      <!-- 1.header中的插槽 -->
      <template #headerHandler>
        <el-button type="primary" size="medium" v-if="isCreate" @click="handleNewClick"
          >新建用户</el-button
        >
      </template>
      <template #status="scope">
        <el-button :type="scope.row.enable ? 'success' : 'danger'">
          {{ scope.row.enable ? '启用' : '禁用' }}
        </el-button>
      </template>
      <template #createAt="scope">
        <span>{{ $filters.formatTime(scope.row.createAt) }}</span>
      </template>
      <template #updateAt="scope">
        <span>{{ $filters.formatTime(scope.row.updateAt) }}</span>
      </template>
      <template #handler="scope">
        <el-button type="text" v-if="isUpdate" @click="handleEditClick(scope.row)">编辑</el-button>
        <el-button type="text" v-if="isDelete" @click="handleDeleteClick(scope.row)">
          删除
        </el-button>
      </template>
      <!-- 在page-content中动态插入剩余的插槽 -->
      <template v-for="item in otherPropsSlots" :key="item.prop" #[item.slotName]="scope">
        <slot :name="item.slotName" :row="scope.row"></slot>
      </template>
    </hy-table>
  </div>
</template>
<style lang="less" scoped></style>
